<?php
namespace app\api\controller\login;

use app\api\controller\Member;
use app\common\model\CourseAttendanceModel;
use app\common\model\CourseModel;
use app\common\model\SchoolClassModel;
use app\common\model\SchoolGradeModel;
use app\common\model\UserModel;
use think\db;

class Classmaster extends Member
{

    /**
     * 班主任的话计算学生数与课程数
     */
    public function getTimeCount(){
        $user = $this->getMember();

        $student_count = 0;
        $course_count = 0;
        $attendance_count = 0;
        $attendance = [];
        if($user['grade_id'] > 0 && $user['class_id'] > 0){
            $student_count = UserModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->where('role',1)->count();

            $course_count =  CourseModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->count();

            //缺勤率
            $course_ids = CourseModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->field('group_concat(id) as course_ids')->find()['course_ids'];
            $attendance_count = CourseAttendanceModel::where('course_id','in',$course_ids)->count();
            $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)->select();

        }
        $result['student_count'] = $student_count;
        $result['course_count'] = $course_count;

        $rate = 0;
        foreach ($attendance as $key=>&$value){
            if(count(explode(',',$value['all_userids']))!=0){
                $rate += count(explode(',',$value['away_userids']))/count(explode(',',$value['all_userids']));
            }
        }
        if($attendance_count!=0){
            $attendance_rate = $rate/$attendance_count;
        }else{
            $attendance_rate = 0;
        }
        $result['attendance_rate'] = round($attendance_rate,4)*100;
        $this->success('获取班主任的学生数与课程数与缺勤率成功！',$result,200);
    }

    /**
     * 班主任的话计算缺勤详细情况
     */
    public function getAttendance(){
        $user = $this->getMember();
        //得到班主任所管的班级的课程
        $course_ids = CourseModel::where('school_id',$user['school_id'])
            ->where('grade_id',$user['grade_id'])
            ->where('class_id',$user['class_id'])
            ->field('group_concat(id) as course_ids')->find()['course_ids'];
        //得到考勤列表
        $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)
            ->order('create_time desc')->select();
        //循环拼成数组
        $all_usernames = '';
        $away_usernames = '';
        foreach ($attendance as $key=>&$value){
            $value['key'] = $key+1;
            $course = CourseModel::where('id',$value['course_id'])->find();
            $value['course_name'] = $course['name'];
            $value['teacher_name'] = UserModel::where('id',$course['teacher_id'])->value('realname');
            $value['class_name'] = SchoolClassModel::where('id',$course['class_id'])->value('name');
            $value['grade_name'] = SchoolGradeModel::where('id',$course['grade_id'])->value('name');
            $all_userids_arr = explode(',',$value['all_userids']);
            foreach ($all_userids_arr as $k1=>$v1){
                //报名的学生
                $all_usernames = $all_usernames .' '.UserModel::where('id',$v1)->value('realname');
            }
            $away_userids_arr = explode(',',$value['away_userids']);
            foreach ($away_userids_arr as $k2=>$v2){
                //缺勤的学生
                $away_usernames = $away_usernames .' '.UserModel::where('id',$v2)->value('realname');
            }
            $value['all_usernames'] = $all_usernames;
            $value['away_usernames'] = $away_usernames;
        }
        $this->success('获取班主任所在班级的缺勤详细情况成功！',$attendance,200);
    }

    /**
     * 获取班主任的学生
     */
    public function getStudent()
    {
        $user = $this->getMember();
        $student = [];
        if($user['grade_id'] > 0 && $user['class_id'] > 0){
            $student = UserModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->where('role',1)->select();
        }

        $this->success('获取班主任的学生成功！',$student,200);
    }

}